package com.example.thread.base.demo4;

import javax.annotation.security.RunAs;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author LiYuhang
 * @version 0.1
 * @application  BlockingQueue实现生产者消费者
 * @Date 2020/12/16 9:51
 */
public class demo4_2 {
    public static void main(String[] args) {
        BlockingQueue<Object> queue=new ArrayBlockingQueue<>(10);
        Runnable producer=()->{
                while (true){
                    try {
                        queue.put(new Object());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
        };
        new Thread(producer).start();
        new Thread(producer).start();
        Runnable consumer=()->{
            while (true){
                try {
                    System.out.println(queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(consumer).start();
        new Thread(consumer).start();
    }
}
